requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        jquery: 'jquery.min',
        jquery_route: 'jquery.route',
        bootstrap: "../../bootstrap-3.3.5/dist/js/bootstrap.min",
        bootstrap_paginator:"../plugins/bootstrap-paginator/bootstrap-paginator",
        validform:"../plugins/Validform/Validform_v5.3.2",
        config: "../common/config",
        common: "../common/common",
        arttemplateUtil: '../common/arttemplate-util',
        stringUtil: "../common/string-util",
        task:"../common/task",
        pageJs: '../common/pageJs',
        commonUtil: '../common/common-util',
        goodsCommon:"../modules/goods/goods_common"
    },
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'jquery': {
            exports: '$'
        },
        'bootstrap': ['jquery'],
        'bootstrap_paginator':{
        	deps:['bootstrap'],
        	exports: 'bootstrp'
        }
    }
});
requirejs(['jquery', 'pageJs','config', 'common','arttemplateUtil','stringUtil','task','validform','commonUtil','goodsCommon'], function($, pageJs,config,common,arttemplateUtil,stringUtil,task,validform,commonUtil,goodsCommon) {
	
	var queryStatus = false;//是否需要传入查询参数
	
	//获取查询时需要的参数
	var getQueryFormParams = function() {
		var formParam = {};
		formParam.goods_id = $("input[name=goods_id]").val();
		formParam.goods_name = $("input[name=goods_name]").val();
		formParam.beginpric = $("input[name=beginpric]").val();
		formParam.endpric = $("input[name=endpric]").val();
		formParam.beginnum = $("input[name=beginnum]").val();
		formParam.endnum = $("input[name=endnum]").val();
		return formParam;
	}
	
	//分页查找商品列表信息
	var queryGoodsPageList = function(page) {
		var index = common.loading();
		var params = {};
		//查询表单入参
		if(queryStatus) {
			params = getQueryFormParams();
		}
		params.pageNo = page;
		common.common_ajax_request('/api/v1/queryPageGoodsInWarehouseMsgList', params, false, function(data){
			data.url=config.imgRoot();
			//渲染商品列表信息开始
			var html = arttemplateUtil.template('goods_list', data);
			$("#goods_content_list").html(html);
			pageJs.setPage(data.pageNo,data.totalPage,data.totalCount,callback);
			window.layer.close(index);
		},'','post');
	}
	
	//分页查找信息服务端成功返回后执行的操作
	var callback = function(page){
		queryGoodsPageList(page);
	}

	//商品上架功能
	var goodsOnShelve = function(mulitId) {
		if(mulitId != "") {
			task.openConfirmTask('确定上架商品?',function() {
				//点击"确认"按钮响应事件
				common.common_ajax_request('/api/v1/goodsSelf.htm',{
					mulitId:mulitId,
					flag:1
				}, false, function(data,code,businessMsg){
					if(businessMsg.businessCode == 0) {
						//刷新商品列表信息
						var currentPage = $("#page").find(".active").find("a").html();
						queryGoodsPageList(currentPage);
						window.layer.msg("上架成功，需要查看，前往出售中的商品");
					} else {
						window.layer.msg(businessMsg.businessNote);
					}
				});
			});
		} else {
			task.modal_alert("请至少选择一条数据记录");
		}
	}
	
	//点击上方可以多选的上架按钮响应
	var goodsMulitIdOnShelveBtnClick = function() {
		$(document).off('click',"#goods_on_shelve").on('click',"#goods_on_shelve",function() {
			var mulitId="";
			$(":checkbox[name='goods_check']:checked").each(function(){
			  if($(this).val()!=""){	  
			    mulitId=mulitId+$(this).val()+",";
			  }
			});
			goodsOnShelve(mulitId);
			//a标签的点击事件要加个返回值，false时禁用到点击时控件随机的链接跳转
			return false;
		});
	}
	
	//点击商品列表中单个商品的上架按钮响应事件
	var goodsSigleOnShelveBtnClick = function() {
		$(document).off('click',".goods_single_on_shelve").on('click',".goods_single_on_shelve",function() {
			//获取商品id
			var goodsId = $(this).attr("data-id");
			goodsOnShelve(goodsId);
		});
	}
	

	
	var query_demo = $("#query_form").Validform({
		tiptype:3,
		isShowRight:false,
		datatype: {
			"saleCompare": function(gets,obj,curform,regxp){ //销量比较
				var minsale = $("input[name=beginnum]").val();
				var maxsale = $("input[name=endnum]").val();
				var reg = /^\s*$|^[1-9]\d{0,8}$|^0$/;
				
				if((stringUtil.isNotEmpty(minsale) && !reg.test(minsale)) || (stringUtil.isNotEmpty(maxsale) && !reg.test(maxsale))) {
					return "请输入[0-999999999]整数";
				}
				
				if(stringUtil.isNotEmpty(minsale) && stringUtil.isNotEmpty(maxsale)) {
					if (parseInt(maxsale) < parseInt(minsale)) {
						return "总销量区间范围错误";
					}
				}
				$("input[name=beginnum]").removeClass("Validform_error");
				$("input[name=endnum]").removeClass("Validform_error");
				return true;
			},"priceCompare": function(gets,obj,curform,regxp){ //销量比较
				var minPrice = $("input[name=beginpric]").val();
				var maxPrice = $("input[name=endpric]").val();
				
				if((stringUtil.isNotEmpty(minPrice) && !commonUtil.isPrice(minPrice)) || (stringUtil.isNotEmpty(maxPrice) && !commonUtil.isPrice(maxPrice))) {
					return "格式错误,价格范围在【0-99999.99】";
				}
				
				if(stringUtil.isNotEmpty(minPrice) && stringUtil.isNotEmpty(maxPrice)) {
					if (parseFloat(maxPrice) < parseFloat(minPrice)) {
						return "当前价格区间范围错误";
					}
				}
				$("input[name=beginpric]").removeClass("Validform_error");
				$("input[name=endpric]").removeClass("Validform_error");
				return true;
			}
        }
	});
	
	//商品搜索方法
	var goodsSearch = function() {
		queryStatus = true;
		queryGoodsPageList();
		return false;
	}
	
	//搜索按钮响应事件
	var goodsSearchBtnClick = function() {
		$("#search_btn").on("click",function(){
			//表单验证通过，执行搜索操作
			if(query_demo.check()) {
				goodsSearch();
			}
			return;
		});
	}
	
	//查询条件重置按钮响应
	var resetBtnClick = function() {
		$("#reset_btn").off('click').on("click",function(){
			$("input[name=goods_id]").val('');
			$("input[name=goods_name]").val('');
			$("input[name=beginpric]").val('');
			$("input[name=endpric]").val('');
			$("input[name=beginnum]").val('');
			$("input[name=endnum]").val('');
			$("#query_form input").removeClass("Validform_error");
			$("#query_form .Validform_checktip").html("").removeClass("Validform_wrong");
			//$(this).attr("type","reset");
			//条件重置完要重新更新商品列表
			goodsSearch();
			queryStatus = false;
			return false;
		});
	}
	
	/***********************************************点击删除按钮删除商品*********************************/
	var goodsDeleteBtnClick = function() {
		//先解除绑定，保证每次只绑定一次
		$(document).off('click',"#goods_remove").on('click',"#goods_remove",function() {
			var mulitId="";
			$(":checkbox[name='goods_check']:checked").each(function(){
			  if($(this).val()!=""){	  
			    mulitId=mulitId+$(this).val()+",";
			  }
			});
			if(mulitId != "") {
				task.openConfirmTask('确认删除商品?',function() {
					//点击"确认"按钮响应事件
					common.common_ajax_request('/api/v1/goodsDel.htm',{
						mulitId:mulitId
					}, false, function(data,code,businessMsg){
						if(businessMsg.businessCode == 0) {
							//刷新商品列表信息
							var currentPage = $("#page").find(".active").find("a").html();
							queryGoodsPageList(currentPage);
							window.layer.msg("删除成功");
						} else {
							window.layer.msg(businessMsg.businessNote);
						}
					});
				});
			} else {
				task.modal_alert("请至少选择一条数据记录");
			}
			//a标签的点击事件要加个返回值，false时禁用到点击时控件随机的链接跳转
			return false;
		});
	}
	
	var init = function() {
		queryGoodsPageList(); 
		goodsCommon.loadConformTaskHtml();
		goodsMulitIdOnShelveBtnClick();
		goodsSigleOnShelveBtnClick();
		goodsSearchBtnClick();
		resetBtnClick();
		goodsDeleteBtnClick();
		goodsCommon.checkAllOrNotClick();
		goodsCommon.checkBtnClick();
		goodsCommon.goodsInventoryUpdateBtnClick();
		goodsCommon.goodsPriceUpdateBtnClick();
		goodsCommon.goodsSingleEditBtnClick();
		goodsCommon.goodsYFPriceUpdateBtnClick(".goods_yunfei_price_update_btn");
    }
	
    init();
});
